package com.filyar.htyd.utils;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author FeiLiYa :Feiliya
 * @date : 2023/12/13 17:55
 * @e-mail:root@filyar.com
 */
@Component
public class RedisUtils {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public void setValueToRedis(String prefix,String key,String value){
        stringRedisTemplate.opsForValue().set(prefix+"-"+key,value);
    }
    public void setValueToRedis(String prefix, String key, String value, long expireTime, TimeUnit timeUnit){
        stringRedisTemplate.opsForValue().set(prefix+"-"+key,value,expireTime,timeUnit);
    }
    public void setValueToRedis(String key,String value){
        stringRedisTemplate.opsForValue().set(key,value);
    }

    public String getValueFromRedis(String prefix,String key){
        return stringRedisTemplate.opsForValue().get(prefix+ "-" + key);
    }
    public String getValueFromRedis(String key){
        return stringRedisTemplate.opsForValue().get(key);
    }

    public Boolean checkKeyExist(String key){
        return stringRedisTemplate.hasKey(key);
    }

    public void deleteKey(String key){
        stringRedisTemplate.delete(key);
    }

    public Boolean hasKey(String prefix,String key){
        return stringRedisTemplate.hasKey(prefix+"-"+key);
    }


}
